package com.mongo.data.controller;

import com.mongo.data.entity.MissionStatus;
import com.mongo.data.entity.NodeInfo;
import com.mongo.data.entity.UploadNode;
import com.mongo.data.service.MissionStatusService;
import com.mongo.data.utils.BeanUtil;
import com.mongo.data.vo.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api
@RestController
@Slf4j
@RequestMapping("rest/v1/job")
public class MissionController {

    @Autowired
    private MissionStatusService service;

    @PostMapping("/select/all")
    public List<MissionStatus> selectAll() {
        return service.selectAll();
    }

    @PostMapping("/create/mission/status")
    public MissionStatus create(@RequestBody MissionStatusCreateVO createVO) {
        MissionStatus missionStatus = BeanUtil.generateTarget(createVO, MissionStatus.class);
        missionStatus.getUploadNode().setUploadTime(new Date().getTime()/1000);
        return service.create(missionStatus);
    }

    @PostMapping("/select/page")
    public PageResult<MissionStatusRespVO> getStatusVOPage(@RequestBody MissionStatusQueryVO queryVO) {
        PageResult<MissionStatusRespVO> statusVOPage = service.getStatusVOPage(queryVO);
        log.info("res is {}" ,statusVOPage.getRecords().toString());
        return  statusVOPage;
    }

    @PostMapping("/select/upload/node/{id}")
    public UploadNode selectUploadNode(@PathVariable Long id) {
        return service.selectUploadNode(id);
    }

    @PostMapping("/select/info/list")
    public List<NodeInfo> selectNodeInfoList(@RequestBody StatusQueryVO statusQueryVO) {
        return service.listNodeInfo(statusQueryVO);
    }

    @PostMapping("/select/info/page")
    public PageResult<NodeInfo> selectNodeInfoPage(@RequestBody StatusQueryVO statusQueryVO) {
        return service.pageNodeInfo(statusQueryVO);
    }

    @PostMapping("/select/info/{id}/{name}")
    public NodeInfo selectNodeInfo(@PathVariable Long id, @PathVariable String name) {
        return service.selectNodeInfo(id, name);
    }

    @PostMapping("/update/end/time/{id}/{endTime}")
    public void updateBigBagEndTime (@PathVariable Long id, @PathVariable Long endTime) {
            service.updateBigBagEndTime(id, endTime);
    }

    @PostMapping("/insert/upload/time/{id}")
    public void insertUpdateTime(@PathVariable Long id) {
        service.insertUploadTime(id);
    }

    @PostMapping("/remove/{id}")
    public void removeById(@PathVariable Long id) {
        service.removeById(id);
    }

    @PostMapping("/test/{id}")
    public void test(@PathVariable Long id) {
        service.updateDataNodeById(id);
    }
}
